import React, { Suspense, useEffect } from 'react';
import { connect } from 'react-redux';
import { Route, Switch, Redirect } from 'react-router-dom';
import storage from './utils/storage';
import routerConfig from './router';
import Loading from './components/Loading';
import Main from './Main';
import micro from './micro';

function App(props) {
  useEffect(() => {
    queryConfigCode();
    queryOrg();
  }, []);
  // 查询全局码表
  const queryConfigCode = () => {
    props.dispatch({ type: 'code/queryConfigCode', params: {} });
  };
  // 查询机构
  const queryOrg = () => {
    props.dispatch({ type: 'global/queryOrg', params: {} });
  };

  return (
    <>
      <Switch>
        {/* <Route
          path='/riskDashboard'
          render={(props) => {
            return <RiskDashboard {...props} />;
          }}
        /> */}
        <Main {...props}>
          {routerConfig.map(({ component: Component, path, redirect, auth, exact, name, des, meta = {} }, key) => {
            return (
              <Route
                path={path}
                exact={exact}
                key={key}
                render={(props) => {
                  if (meta) {
                    micro.setGlobalState({ meta });
                  }
                  // if (redirect) {
                  //   return <Redirect to={redirect} />;
                  // }
                  // if (auth) {
                  //   if (storage.token) {
                  //     return (
                  //       <Suspense fallback={<span />}>
                  //         <Component {...props} />
                  //       </Suspense>
                  //     );
                  //   } else {
                  //     return <Redirect to='/login' />;
                  //   }
                  // } else {
                  return (
                    <Suspense fallback={<Loading loading={true} />}>
                      <Component {...props} />
                    </Suspense>
                  );
                  // }
                }}
              />
            );
          })}
        </Main>
      </Switch>
    </>
  );
}
export default connect()(App);
